/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  FontLoader
 * 
 * Description of class:
 * 
 * Loads the default fonts for the system.
 * 
 */


package karel.util;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;


public class FontLoader {
    
    public static int TextEditorFontSize = 16;
    public static int ConsoleFontSize = 14;
    
    public static int ButtonFontSize = 14;
    public static int ButtonFontType = Font.BOLD;
    
    
    
    private static boolean fontLoaded = false;
    private static String defaultFont = "Monospaced";
    private static String OS;
    
    public static Font getDefaultFont(int style, int size) {
        if(!fontLoaded) {
            fontLoaded = true;
            try {
                //InputStream is = FontLoader.class.getResourceAsStream("fName");
                if(!isWindows()) {
                    defaultNotLoaded();
                } else {
                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    registerFont(ge,FontLoader.class.getResourceAsStream("/font/DejaVuSansMono.ttf"));
                    registerFont(ge,FontLoader.class.getResourceAsStream("/font/DejaVuSansMono-Bold.ttf"));
                    registerFont(ge,FontLoader.class.getResourceAsStream("/font/DejaVuSansMono-BoldOblique.ttf"));
                    registerFont(ge,FontLoader.class.getResourceAsStream("/font/DejaVuSansMono-Oblique.ttf"));
                    defaultFont = "DejaVu Sans Mono";
                }
            } catch (Exception ex) {
                FontLoader.defaultNotLoaded();
            }
        }     

        return new Font(defaultFont, style, size);
    }
    
    private static void defaultNotLoaded() {
        System.err.println("DejaVu Sans Mono not loaded.  Using "+defaultFont);
    }
    
    private static void registerFont(GraphicsEnvironment ge, InputStream in) throws FontFormatException, IOException {
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT,in));
    }
    
    private static String getOsName() {
        if(OS == null) { OS = System.getProperty("os.name"); }
        return OS;
    }
    
    private static boolean isWindows() {
      return getOsName().startsWith("Windows");
    }
}
